package com.csh.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.csh.subject.application.convert.SubjectCategoryDtoConvert;
import com.csh.subject.application.dto.SubjectCategoryDto;
import com.csh.subject.common.entity.Result;
import com.csh.subject.domain.entity.SubjectCategoryBo;
import com.csh.subject.domain.service.SubjectCategoryDomainService;
import com.csh.subject.infra.basic.entity.SubjectCategory;
import com.csh.subject.infra.basic.service.SubjectCategoryService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;


/**
 * 题目分类controller
 * @Author: Maverick_Csh
 * @Date: 2023/12/24
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {

    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @GetMapping("/test")
    public String test(){
        SubjectCategory subjectCategory = subjectCategoryService.queryById(1L);
        return subjectCategory.getCategoryName();
    }

    /**
     *新增题目分类
     * @param subjectCategoryDto
     * @return
     */
    @PostMapping ("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDto subjectCategoryDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDto.getCategoryName()), "分类名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDto.getCategoryType(),"分类类型不能为空");
            Preconditions.checkNotNull(subjectCategoryDto.getParentId(),"分类父级id不能为空");
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConvert.INSTANCE
                    .convertDtoToBo(subjectCategoryDto);
            subjectCategoryDomainService.add(subjectCategoryBo);
            return Result.ok(true);
        }catch (Exception e){
            log.error("SubjectCategoryController.add.error:{}",e.getMessage(),e);
            return Result.fail("新增题目分类失败");
        }
    }

    /**
     *查询题目大类
     * @return
     */
    @PostMapping ("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDto>> queryPrimaryCategory(@RequestBody SubjectCategoryDto subjectCategoryDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectCategoryController.queryPrimaryCategory.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            Preconditions.checkNotNull(subjectCategoryDto.getCategoryType(),"分类的类型不能为空");
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConvert
                    .INSTANCE.convertDtoToBo(subjectCategoryDto);
            List<SubjectCategoryBo> subjectCategoryBoList = subjectCategoryDomainService.queryCategory(subjectCategoryBo);
            List<SubjectCategoryDto> subjectCategoryDtoList = SubjectCategoryDtoConvert
                    .INSTANCE.convertBoToDtoList(subjectCategoryBoList);
            return Result.ok(subjectCategoryDtoList);
        }catch (Exception e){
            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}",e.getMessage(),e);
            return Result.fail("查询岗位分类失败");
        }
    }

    /**
     * 查询大类下的分类
     * @param subjectCategoryDto
     * @return
     */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDto>> queryCategoryByPrimary(@RequestBody SubjectCategoryDto subjectCategoryDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectCategoryController.queryCategoryByPrimary.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            Preconditions.checkNotNull(subjectCategoryDto.getParentId(),"子分类中的父分类id不能为空");
            Preconditions.checkNotNull(subjectCategoryDto.getCategoryType(),"分类类型不能为空");
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConvert
                    .INSTANCE.convertDtoToBo(subjectCategoryDto);
            List<SubjectCategoryBo> subjectCategoryBoList = subjectCategoryDomainService.queryCategory(subjectCategoryBo);
            List<SubjectCategoryDto> subjectCategoryDtoList = SubjectCategoryDtoConvert
                    .INSTANCE.convertBoToDtoList(subjectCategoryBoList);
            return Result.ok(subjectCategoryDtoList);
        }catch (Exception e){
            log.error("SubjectCategoryController.queryCategoryByPrimary.error:{}",e.getMessage(),e);
            return Result.fail("查询岗位大类失败");
        }
    }

    /**
     * 更新题目分类
     * @return
     */
    @PutMapping ("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDto subjectCategoryDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectCategoryController.update.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDto.getCategoryName()), "分类名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDto.getCategoryType(),"分类类型不能为空");
            Preconditions.checkNotNull(subjectCategoryDto.getParentId(),"分类父级id不能为空");
            Preconditions.checkNotNull(subjectCategoryDto.getId(),"分类id不能为空");
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConvert
                    .INSTANCE.convertDtoToBo(subjectCategoryDto);
            Boolean result = subjectCategoryDomainService.update(subjectCategoryBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectCategoryController.update.error:{}",e.getMessage(),e);
            return Result.fail("更新分类信息失败");
        }
    }

    /**
     * 删除题目分类（逻辑删除）
     * @param subjectCategoryDto
     * @return
     */
    @PutMapping ("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDto subjectCategoryDto){
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectCategoryController.delete.dto:{}", JSON.toJSONString(subjectCategoryDto));
            }
            Preconditions.checkNotNull(subjectCategoryDto.getId(),"分类id不能为空");
            SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConvert
                    .INSTANCE.convertDtoToBo(subjectCategoryDto);
            Boolean result = subjectCategoryDomainService.delete(subjectCategoryBo);
            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectCategoryController.delete.error:{}",e.getMessage(),e);
            return Result.fail("删除分类失败");
        }
    }
}
